package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @Classname AlipayTemplate
 * @Description TODO
 * @Date 2021/10/21 11:40
 * @Author Anonymous
 */

@SuppressWarnings("all")
@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    // 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
    //https://openhome.alipay.com/platform/appDaily.htm?tab=info
    public String app_id = "2021000118635886";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCtTDGeCcoKDW2Phz37ikfVTtjrJ4Qgahk4xnrq4DV8S69ZZAJOKMxnH9dXABtIezy5Y8xTkjIAt+MuaMXBZqY0/NcAq/hfCc6LWLPIeeuA/AgB+2YqSeIQ4JJ9z3bJ710LCaZ3pGRczSJzZZnlM42MXsudmnnJWGiQKZJNJLuM3t6Wu7hrrTw6UAjPAmpCC65pA9OkHiNg2fHjZQS6YbJt7WyKX9EUUgcryfPIVkFoklFOkimCWDUF69Xk6wa4gmT8vdRZBPtQu9mLCIKDaVyK4F/NoUAuZpn3KYdz271fWxjlbUkIRtvCLjqyF2Ry5m8TK0c82oXScvdJ3l8D2lypAgMBAAECggEALgSsrFXXI+I69MEqEXC/8IHWkmNU77Kc7fmuzE4rhc0nTCqFfjHRTZUj/sNVe2LWVjiNGqgYxR4PotRJpzjV6/40LcZ81ufoxBIS86qEgiTrD3Otxo8GDAtGe0grOCZq47J1VRH/YLFMc65INpgA/4Rwt3I84t2qLpYLdpbJgvd/wY6/Uk6q8wsJmSfWx4fGVErJRpXbW/oKkow45O7McSfe5/7ONsX8wtB6Y4c4DJKhp+EQVtWMs9oWepkEYeoEeqjn64dhUmve6EmaDkk2vROHKlm6XMsMFjFAyKCT1plVtmFlv1z4iIvr6iwZ6F6BJJWIj3PVVZ0JVSz1RxiLLQKBgQDfGyOe2efis5gQf4C6+C94Xv/hQDe2bSEgiisAgELFgfOz5oWdPK3DTEcJe+mOOGmp57rtigCrXjiEv31QIywwP2DzJ+5cwBatwIEXTv4z708s7X2R97/RHvvZCMBMotCtNTK//vZnYoH6eqR6tPRCsOlPPb9DMUr8XhZqRW+ggwKBgQDG2RlQs7yX6oMfL8F3/N/CH1uraJNyWmSBjWCBoOAbmP28d3YkKuXdRePlXty1fGfu9XldqGEisXEOyyU1F15C6QgNejzADFMv0cJE0+bPuZYqzS37WHqNkusTBcdV+eiiYn+zSY61k3XgIPi6z1AELgbgSLXHQ5Rovlob5r1uYwKBgQCgpPT4C2T5imFi97vkZJIhtR2/vjKfa2KgAHzIJpJtjo3AqqrotelUJ1c7D/ryydHjYhTptyWWPwipJQyBKGJH90rCqqzqIfHyeBs7gkhtctHqwrJY6TA3znx/INr+E3n4TcbWEiqv9HEsvyQxqpv6bVIBGa/fHN4ma0TujkK7KwKBgBjPY6I3GtPVldChU8FV0tFUukpTvPnqn1muuxjRasTmvNn0CdCPbajDHKE0ydL3F7yv8hmmdaAvhYyz+gt6w+8TRkbwNn47y3FO+a8+xTCr1jm26gLPUlMxvpTcBrgKe2UvZwGGrwCmynFRi9c7IJ+04nS/fbbEDtNW1MVgb8hDAoGBAJ8hGf07fYSGatX5STFHSYMKD0dFxyHlyJG9lqDmICwPAGR9w/U/s/1gP4KzFRKFr0G2oEUGnzC3kJ1mhBmlMIjvvnnu7jvOfupOWQPcFSaAl6wj3RSvydIgTl9/vXt6ZETCFBTZIOesc9Sn95QyUPR+9a47PtDpl2BKjTC1Zqv7";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApuMwB5Va3gxy5qRL6Cx7xQPVKSNc8hkyIsVtEqhlq5mJmJtOO9Ih0zGSdIm+8NILkMqax2vxoh8cbv/DW0eVsR2ZZ5/f4TKquDDtfU9lWo8JXJNBI+edlMulRRBcxSpnFdglfRgYVT2AxQOxwsR+RksYZEAuPs4MC9zaMCqJr+H95GPRf/lTZKwdOBuhD/te4suhomRKTz+HX7PabNJvWq5yk+wnwhWCI844u75/T2AKZEie9haOdqspoFAsVHDZib3tZTn94laC54qOFHAQsTl9+uNV2i1vtAsUtytZyta12iZ5XE8BY3/+Z5vb3ejdLkuPKGbeTKo/NPBejws+0wIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    public String notify_url = "http://hij2ki8yy9.51xd.pub/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    public String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    //订单超时时间
    private String timeout = "5m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    public String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {
        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝响应：登录页面的代码\n" + result);

        return result;
    }
}
